import {_decorator, Component, Node, log, RigidBody, Vec3,Prefab,
    instantiate,
    PhysicsSystem
} from 'cc';

const {ccclass, property} = _decorator;


const DropDistance:number = 3;

@ccclass('Helloworld')
export class Helloworld extends Component {
    /* class member could be defined like this */
    // dummy = '';

    /* use `property` decorator if your want the member to be serializable */
    // @property
    // serializableDummy = 0;

    @property({type: Node})
    nd_map: Node = null;

    @property({type: Node})
    nd_btn: Node = null;

    @property({type: Node})
    nd_ball: Node = null;

    @property({type: Node})
    nd_floorParent: Node = null;

    @property({type:Prefab})
    pf_floor:Prefab = null;

    //-------------------------------------------
    _degressY: number = 0;
    _lstNdFloor: Node[] = [];

    onLoad(){

        let ins = PhysicsSystem.instance;
        ins.enable = false;
        ins.gravity = new Vec3(0,-20,0);
        ins.maxSubSteps = 1;

        // 创建10个
        for(let i = 0 ; i < 10; ++i){
            let nd_floor = instantiate(this.pf_floor);
            nd_floor.setParent(this.nd_floorParent);
            this._lstNdFloor.push(nd_floor);
            nd_floor.setPosition(0, -DropDistance * i,0 );
            let randomDegress = ~~(Math.random() * 360);
            nd_floor.setRotationFromEuler(0,randomDegress,0);
        }

        // 设置位置和角度
        this._lstNdFloor[0].setRotationFromEuler(0,90,0);
    }

    start() {
        this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        log(this.nd_map.rotation);
    }

    onTouchMove(event) {
        log("onTouchMove called!");
        // log("event:",event);
        let delta = event.getDelta();
        if (delta.x > 0) {
            log("从左向右");
        } else if (delta.x < 0) {
            log("从右向左");
        }

        // 旋转
        this._degressY += delta.x;
        this.nd_map.setRotationFromEuler(0, this._degressY, 0);
    }

    // update (deltaTime: number) {
    //     // Your update function goes here.
    // }

    play_btnClicked() {
        log('play_btnClicked called!');
        this.nd_btn.active = false;

        PhysicsSystem.instance.enable = true;
    }
}
